package calorie.com.calorie.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import calorie.com.calorie.R;
import calorie.com.calorie.adapter.ConsumptionRecordAdapter;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.ConsumptionContact;
import calorie.com.calorie.response.ConsumptionRecordResponce;
import retrofit2.Response;

//消费
public class ConsumptionRecordFragment extends MvpFragment<ConsumptionContact.ConsumptionPresent> implements ConsumptionContact.IConsuptionView {
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    List<ConsumptionRecordResponce.DataBean> mList;
    ConsumptionRecordAdapter mAdapter;
    private int pageNo=1;

    @Override
    public void initData(Bundle savedInstanceState) {
        mList=new ArrayList<>();
        mAdapter=new ConsumptionRecordAdapter(R.layout.recy_item_consumption_record_list,mList);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        rv.setLayoutManager(manager);
        rv.setAdapter(mAdapter);

        mPresent.queryConsuptionRecord(context,pageNo);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo=1;
                mPresent.queryConsuptionRecord(context,pageNo);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNo++;
                mPresent.queryConsuptionRecord(context,pageNo);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_consumption_record;
    }


    @Override
    public void getConsuptionRecord(Response<ConsumptionRecordResponce> response) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            List<ConsumptionRecordResponce.DataBean> data = response.body().getData();
            if (pageNo==1){
                mList.clear();
            }
            mList.addAll(data);
            mAdapter.notifyDataSetChanged();
            if (!ObjectUtils.isEmpty(data)&&data.size()>=10){
                refresh.setEnableLoadMore(true);
            }else {
                refresh.setEnableLoadMore(false);
            }
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    protected ConsumptionContact.ConsumptionPresent createPresent() {
        return new ConsumptionContact.ConsumptionPresent();
    }
}
